<?php

session_start();

require_once('security.php');
require_once('../../config.php');

if (!isset($_SESSION['thispage'])) {$_SESSION['thispage']="order_menu"; }
if (isset($_GET["thispage"])){ $_SESSION['thispage']=htmlspecialchars($_GET["thispage"]);}
if (isset($_GET["field"])){ $_SESSION['field']=htmlspecialchars($_GET["field"]);}
if (isset($_GET["delim"])){ 
	if ($_GET['delim'] == ''){
		$_SESSION['delim']=' ';
	}else if ($_GET['delim'] == '%20'){
		$_SESSION['delim']=' ';
	}else{
		$_SESSION['delim']=htmlspecialchars($_GET['delim']);
	}
if (isset($_GET["file"])){ $_SESSION['file']=htmlspecialchars($_GET["file"]);}
if (isset($_GET["thispage"])){ $_SESSION['thispage']=htmlspecialchars($_GET["thispage"]);}
}
unset($_GET['file']);
unset($_GET['field']);
unset($_GET['delim']);

include_once('functions.php');
$done=0;

if (isset($_POST['doorder']) && $_POST['doorder'] != "" ){
	$point=$_POST['doorder'];
	if (isset($_POST['up'])){
		$_SESSION['zarray']=moveup($_SESSION['zarray'],$_POST['doorder'],$_SESSION['field'],$_SESSION['delim'],$_SERVER['PHP_SELF']);
		$zarray=$_SESSION['zarray'];
		$done++;
	}else if (isset($_POST['down'])){
		$_SESSION['zarray']=movedown($_SESSION['zarray'],$_POST['doorder'],$_SESSION['field'],$_SESSION['delim'],$_SERVER['PHP_SELF']);
		$zarray=$_SESSION['zarray'];
		$done++;
	}
}

//echo "got here 1" .$_SESSION['file'];

if (isset($_POST['save'])){

	$fh=fopen($_SESSION['file'],"w") or die("Unable to open file: $file"); // Open file for overwrite

	foreach($_SESSION['zarray'] as $line){  // Fill file
		fwrite($fh,$line);
	}
	fclose($fh);  // Close file

	$fh=fopen('../response.txt','w') or die("Unable to open file: response.txt"); // Open file for overwrite
	fwrite($fh,"<b>The Order of file: " .$_SESSION['file'] ." has been saved.</b>");
	fclose($fh);  // Close file

	unset($_SESSION['file']); // Clear SESSION vars
	unset($_SESSION['field']);
	unset($_SESSION['delim']);
	unset($_GET['file']);  // Clear GET vars
	unset($_GET['field']);
	unset($_GET['delim']);

	header('Location: http://cit3.ldl.swin.edu.au/~mask/lecmx/cms/admin/');
}

if ($done==0){
	$zarray=file($_SESSION['file']);
	$_SESSION['zarray']=$zarray;
	$point=-1;

	if (isset($zarray)){
	display($zarray,$_SESSION['field'],$_SESSION['delim'],$_SERVER['PHP_SELF'],$point);
	
	}
if (isset($response)){ echo $response; unset($response);}
//print_r($_SESSION['zarray']);	  
}
?>

<!-- === -->
</div> <!-- wrap -->
</body>
</html>
<!--  Got to End of Order_menu.php -->
